<template>
  <div>
    <div v-for="(item, index) in dataList" :key="index" class="item" @click="handleClick(item)">
      <div class="label-row">
        <span class="name">{{ item.name }}</span>
        <span class="count">{{ item.count }}人</span>
      </div>
      <div :class="['value-row', colors[index % colors.length]]">
        <div class="content" :style="{ width: item.percentage + '%' }"></div>
        <div class="dot-wrap">
          <div class="dot"></div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: "g-horizontal-bar",
  props: {
    dataList: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      colors: ["bg-green", "bg-blue", "bg-yellow", "bg-red"],
    };
  },
  created() {},
  methods: {
    handleClick(item) {
      this.$emit("clickBar", item);
    },
  },
};
</script>
<style scoped lang="scss">
.item {
  width: 100%;
  margin-bottom: 24px;
  padding: 0 6px;
  box-sizing: border-box;
  .label-row {
    width: 100%;
    font-size: 16px;
    color: #ffffff;
    margin-bottom: 10px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    span {
      font-family: STHeitiTC;
    }
    .count {
      color: #00f3ff;
    }
  }
  .value-row {
    width: 100%;
    height: 7px;
    border-radius: 2px;
    display: flex;
    align-items: center;
    .content {
      height: 7px;
      border-radius: 2px;
      transition: width 0.3s ease-in;
    }
    .dot-wrap {
      margin-left: -5px;
      border-radius: 50%;
      .dot {
        width: 6px;
        height: 6px;
        border-radius: 50%;
      }
    }
  }
  // 定义颜色变量
  $colors: (
    "green": (
      end: #39eba5,
    ),
    "blue": (
      end: #8bfdf9,
    ),
    "yellow": (
      end: #ede67b,
    ),
    "red": (
      end: #ff4d4f,
    ),
  );

  // 通用样式mixin
  @mixin color-variant($end) {
    background: rgba(0, 75, 177, 0.18);
    .content {
      background: linear-gradient(90deg, rgba(0, 75, 177, 0.18) 0%, $end 100%);
    }
    .dot-wrap {
      outline: 1px solid rgba($end, 0.7);
      border: 5px solid rgba($end, 0.2);
      .dot {
        background: $end;
      }
    }
  }

  // 使用map遍历生成样式
  @each $name, $color in $colors {
    .bg-#{$name} {
      @include color-variant(map-get($color, end));
    }
  }
}
</style>
